#rm websocket_client.o websocket_client
WEBRTCROOT=/media/luyao/video_send_back/webrtc
WEBRTCOBJS=${WEBRTCROOT}/src/out/Debug/obj
WEBRTCOBJS=${WEBRTCROOT}/src/out/Release/obj
WEBRTCOBJS=${WEBRTCROOT}/src/out/Release_simple/obj

export CC=${WEBRTCROOT}/src/third_party/llvm-build/Release+Asserts/bin/clang
export CXX=${WEBRTCROOT}/src/third_party/llvm-build/Release+Asserts/bin/clang++

defines="-DUSE_UDEV -DUSE_AURA=1 -DUSE_GLIB=1 -DUSE_NSS_CERTS=1 -DUSE_X11=1 -DFULL_SAFE_BROWSING -DSAFE_BROWSING_CSD -DSAFE_BROWSING_DB_LOCAL -DCHROMIUM_BUILD -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_GNU_SOURCE -DCR_CLANG_REVISION=\"365097-f7e52fbd-8\" -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D_FORTIFY_SOURCE=2 -D_LIBCPP_ABI_UNSTABLE -D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS -D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS -D_LIBCPP_ENABLE_NODISCARD -DCR_LIBCXX_REVISION=361348 -DCR_SYSROOT_HASH=e7c53f04bd88d29d075bfd1f62b073aeb69cbe09 -DNDEBUG -DNVALGRIND -DDYNAMIC_ANNOTATIONS_ENABLED=0 -DWEBRTC_ENABLE_PROTOBUF=1 -DWEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE -DRTC_ENABLE_VP9 -DHAVE_SCTP -DWEBRTC_USE_H264 -DWEBRTC_NON_STATIC_TRACE_EVENT_HANDLERS=1 -DWEBRTC_POSIX -DWEBRTC_LINUX -DABSL_ALLOCATOR_NOTHROW=1 -DABSL_FLAGS_STRIP_NAMES=0 -DHAVE_WEBRTC_VIDEO"

cflags="-Wno-deprecated-declarations -fno-strict-aliasing --param=ssp-buffer-size=4 -fstack-protector -funwind-tables -fPIC -B../../third_party/binutils/Linux_x64/Release/bin -pthread -fcolor-diagnostics -fmerge-all-constants -fcrash-diagnostics-dir=../../tools/clang/crashreports -Xclang -mllvm -Xclang -instcombine-lower-dbg-declare=0 -fcomplete-member-pointers -m64 -march=x86-64 -Wno-builtin-macro-redefined -D__DATE__= -D__TIME__= -D__TIMESTAMP__= -no-canonical-prefixes -Wall -Werror -Wextra -Wimplicit-fallthrough -Wthread-safety -Wextra-semi -Wno-missing-field-initializers -Wno-unused-parameter -Wno-c++11-narrowing -Wno-unneeded-internal-declaration -Wno-undefined-var-template -Wno-ignored-pragma-optimize -O2 -fno-ident -fdata-sections -ffunction-sections -fno-omit-frame-pointer -g0 -fvisibility=default -Wheader-hygiene -Wstring-conversion -Wtautological-overlap-compare -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Wc++11-narrowing -Wimplicit-fallthrough -Wthread-safety -Winconsistent-missing-override -Wundef -Wunused-lambda-capture -Wno-shorten-64-to-32 -isystem../../build/linux/debian_sid_amd64-sysroot/usr/include/gtk-3.0 -isystem../../build/linux/debian_sid_amd64-sysroot/usr/include/at-spi2-atk/2.0 -isystem../../build/linux/debian_sid_amd64-sysroot/usr/include/at-spi-2.0 -isystem../../build/linux/debian_sid_amd64-sysroot/usr/include/dbus-1.0 -isystem../../build/linux/debian_sid_amd64-sysroot/usr/lib/x86_64-linux-gnu/dbus-1.0/include -isystem../../build/linux/debian_sid_amd64-sysroot/usr/include/gtk-3.0 -isystem../../build/linux/debian_sid_amd64-sysroot/usr/include/gio-unix-2.0/ -isystem../../build/linux/debian_sid_amd64-sysroot/usr/include/cairo -isystem../../build/linux/debian_sid_amd64-sysroot/usr/include/libdrm -isystem../../build/linux/debian_sid_amd64-sysroot/usr/include/pango-1.0 -isystem../../build/linux/debian_sid_amd64-sysroot/usr/include/harfbuzz -isystem../../build/linux/debian_sid_amd64-sysroot/usr/include/pango-1.0 -isystem../../build/linux/debian_sid_amd64-sysroot/usr/include/fribidi -isystem../../build/linux/debian_sid_amd64-sysroot/usr/include/atk-1.0 -isystem../../build/linux/debian_sid_amd64-sysroot/usr/include/cairo -isystem../../build/linux/debian_sid_amd64-sysroot/usr/include/pixman-1 -isystem../../build/linux/debian_sid_amd64-sysroot/usr/include/uuid -isystem../../build/linux/debian_sid_amd64-sysroot/usr/include/freetype2 -isystem../../build/linux/debian_sid_amd64-sysroot/usr/include/libpng16 -isystem../../build/linux/debian_sid_amd64-sysroot/usr/include/gdk-pixbuf-2.0 -isystem../../build/linux/debian_sid_amd64-sysroot/usr/include/glib-2.0 -isystem../../build/linux/debian_sid_amd64-sysroot/usr/lib/x86_64-linux-gnu/glib-2.0/include"

cflags_cc="-std=c++11 -fno-exceptions -fno-rtti -nostdinc++ -isystem../../buildtools/third_party/libc++/trunk/include -isystem../../buildtools/third_party/libc++abi/trunk/include --sysroot=../../build/linux/debian_sid_amd64-sysroot -fvisibility-inlines-hidden -Wnon-virtual-dtor -Woverloaded-virtual -Wno-deprecated-declarations -Wno-deprecated-declarations -Wno-undef"


#$CXX $cflags $cflags_cc $defines test.cc -L${WEBRTCOBJS} -lc++ -lc++abi

#cd ${WEBRTCOBJS}/buildtools/third_party/libc++/libc++ && ar -crv libc++.a *.o && mv libc++.a ${WEBRTCOBJS}/
#cd ${WEBRTCOBJS}/buildtools/third_party/libc++abi/libc++abi && ar -crv libc++abi.a *.o && mv libc++abi.a ${WEBRTCOBJS}/
#exit

WEBRTCINCLUDE="-I${WEBRTCROOT}/src -I${WEBRTCROOT}/src/third_party/abseil-cpp -I${WEBRTCROOT}/src/third_party/jsoncpp/source/include  -I${WEBRTCROOT}/src/third_party/jsoncpp/generated -I${WEBRTCROOT}/src/third_party/libyuv/include -I${WEBRTCROOT}/src/buildtools/third_party/libc++/trunk/include -I${WEBRTCROOT}/src/buildtools/third_party/libc++abi/trunk/include"

CMAKE_C_OUTPUT_EXTENSION=".o"
WEBRTCEXTRAOBJS="${WEBRTCOBJS}/third_party/jsoncpp/jsoncpp/*${CMAKE_C_OUTPUT_EXTENSION} ${WEBRTCOBJS}/rtc_base/rtc_json/json${CMAKE_C_OUTPUT_EXTENSION} ${WEBRTCOBJS}/api/task_queue/default_task_queue_factory/*${CMAKE_C_OUTPUT_EXTENSION} ${WEBRTCOBJS}/rtc_base/rtc_task_queue_libevent/*${CMAKE_C_OUTPUT_EXTENSION}"

$CXX $WEBRTCINCLUDE $cflags $cflags_cc $defines -c -o easywsclient.o easywsclient.cc
$CXX $WEBRTCINCLUDE $cflags $cflags_cc $defines -c -o conductor.o conductor.cc
$CXX $WEBRTCINCLUDE $cflags $cflags_cc $defines -Wno-implicit-fallthrough -Wno-writable-strings -c -o websocket_client.o websocket_client.cc

WEBRTCEXTRAOBJS="${WEBRTCOBJS}/third_party/jsoncpp/jsoncpp/*${CMAKE_C_OUTPUT_EXTENSION} ${WEBRTCOBJS}/rtc_base/rtc_json/json${CMAKE_C_OUTPUT_EXTENSION} ${WEBRTCOBJS}/api/task_queue/default_task_queue_factory/*${CMAKE_C_OUTPUT_EXTENSION} ${WEBRTCOBJS}/rtc_base/rtc_task_queue_stdlib/*${CMAKE_C_OUTPUT_EXTENSION} ${WEBRTCOBJS}/api/create_peerconnection_factory/create_peerconnection_factory.o"

#ar -crv libwebrtc_peerconnection.a ${WEBRTCEXTRAOBJS} ${WEBRTCOBJS}/buildtools/third_party/libc++/libc++/*.o ${WEBRTCOBJS}/buildtools/third_party/libc++abi/libc++abi/*.o && mv libwebrtc_peerconnection.a ${WEBRTCOBJS}/

g++ conductor.o websocket_client.o easywsclient.o ${WEBRTCEXTRAOBJS} -o libwebrtc_so.so  \
    -fPIC -shared -L${WEBRTCOBJS} \
    -lpthread -lm -lwebrtc -lwebrtc_peerconnection -ldl -lX11
g++ test_so.cc -L. -lwebrtc_so `pkg-config --cflags --libs opencv`
exit

g++ test_so.cc conductor.o websocket_client.o easywsclient.o ${WEBRTCEXTRAOBJS} \
    `pkg-config --cflags --libs opencv` \
    -L${WEBRTCOBJS} \
    -o webrtc -lpthread -lm -lwebrtc -lwebrtc_peerconnection -ldl -lX11
exit

ldflags="-Wl,--fatal-warnings -fPIC -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -Wl,-z,defs -Wl,--as-needed -fuse-ld=lld -Wl,--icf=all -Wl,--color-diagnostics -m64 -Werror -Wl,-O2 -Wl,--gc-sections -rdynamic -nostdlib++ --sysroot=../../build/linux/debian_sid_amd64-sysroot -L../../build/linux/debian_sid_amd64-sysroot/usr/local/lib/x86_64-linux-gnu -L../../build/linux/debian_sid_amd64-sysroot/lib/x86_64-linux-gnu -L../../build/linux/debian_sid_amd64-sysroot/usr/lib/x86_64-linux-gnu -pie -Wl,--disable-new-dtags"
$CXX $WEBRTCINCLUDE $defines $ldflags conductor.o websocket_client.o easywsclient.o ${WEBRTCEXTRAOBJS} \
     -L${WEBRTCOBJS} -L${WEBRTCOBJS}/base/third_party/libevent -L${WEBRTCOBJS}/api \
     -o webrtc -levent -lpthread  -lm -ldl -lX11 -lcreate_peerconnection_factory -lwebrtc -lc++ -lc++abi
